package com.china08.yxyapi.api.manage.sch.platform.resource.yxylectureprepack;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lecture.YxyLectureFeedBack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLectureFeedBackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.util.LectureUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLectureSchPrePackUpdStatusController extends YxyApiBaseController {

    Logger log = Logger.getLogger(YxyLectureSchPrePackStatusController.class);

    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    YxyLectureFeedBackRepository yxyLectureFeedBackRepository;

    @Autowired
    private LectureUtil lectureUtil;

    @ApiOperation(value = "备课包上架下架")
    @RequestMapping(value = "/api_yw/manage/sch/platform/resource/lectureprepack/upd/status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        Integer status = reqModel.status;
        String downReason = reqModel.downReason;

        YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository.findById(id);
        Assert.isTrue(null != yxyLecturePrePack, "备课包为空");
        yxyLecturePrePack.setStatus(status);
        yxyLecturePrePack.setDownReason(downReason);
        yxyLecturePrePack.setAuditUserName(getProfile(request, response).getUserNick());
        yxyLecturePrePack.setAuditUserId(getProfile(request, response).getId());
        yxyLecturePrePack.setAuditSchoolId(getProfile(request, response).getSchoolId());
        yxyLecturePrePack.setAuditSchoolName(getProfile(request, response).getSchoolName());
        yxyLecturePrePackRepository.save(yxyLecturePrePack);

        // 修改所有举报为上/下架
        List<YxyLectureFeedBack> list = yxyLectureFeedBackRepository.findByPrepackIdOrderByCreatedDateDesc(id);
        for (YxyLectureFeedBack yxyLectureFeedBack : list) {

            // 如果下架(status为0)纠错表进行删除操作
            if (status == 0) {
                // YxyLectureFeedBack feedBack = yxyLectureFeedBackRepository
                // .findByPrepackId(yxyLectureFeedBack.getPrepackId());

                yxyLectureFeedBackRepository.delete(list);
                break;
            } else {
                // 如果上架(status为1)纠错表进行修改操作
                yxyLectureFeedBack.setStatus(status);
                yxyLectureFeedBack.setDownReason(downReason);
                yxyLecturePrePack.setAuditUserName(getProfile(request, response).getUserNick());
                yxyLecturePrePack.setAuditUserId(getProfile(request, response).getId());
                yxyLecturePrePack.setAuditSchoolId(getProfile(request, response).getSchoolId());
                yxyLecturePrePack.setAuditSchoolName(getProfile(request, response).getSchoolName());
                yxyLectureFeedBackRepository.save(yxyLectureFeedBack);
            }

        }
        lectureUtil.save(getProfile(request, response), 5, yxyLecturePrePack.getAreaIdDistrict(),
                yxyLecturePrePack.getSchoolId(), yxyLecturePrePack.getCreatedDate());

        lectureUtil.saveYt(yxyLecturePrePack.getAreaIdDistrict(), yxyLecturePrePack.getSchoolId(),
                yxyLecturePrePack.getUserId(), yxyLecturePrePack.getUserNick(), yxyLecturePrePack.getSchoolNick(),
                yxyLecturePrePack.getCreatedDate());

    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;

        public Integer status;

        // 下架原因(逗号分隔)
        public String downReason;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "备课包为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
